package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;


    // @Override
    // public ProductDetailDto getProductDetail(Long id) {
    //     return null;
    // }
    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        if(item == null || itemDesc == null){
            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(),ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
        }
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    // @Override
    // public AllProductResponse getAllProduct(PageRequest pageInfo) {
    //     return null;
    // }
    @Override
    // pageInfo-----page,size,sort,priceGt,priceLte;sort=1,价格低到高；sort=-1,价格高到低；null默认created desc;
    public AllProductResponse getAllProduct(PageRequest pageInfo) {

        PageHelper.startPage(pageInfo.getPage(),pageInfo.getSize());

        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        // status=1，上架中
        criteria.andEqualTo("status",1);
        // cid分类
        if(pageInfo.getCid() != null){
            criteria.andEqualTo("cid",pageInfo.getCid());
        }
        // 价格区间 例如gt--lte,0-99,包含等于
        if(pageInfo.getPriceGt()!=null){
            criteria.andGreaterThanOrEqualTo("price",pageInfo.getPriceGt());
        }
        if(pageInfo.getPriceLte()!=null){
            criteria.andLessThanOrEqualTo("price",pageInfo.getPriceLte());
        }
        // sort=1,价格低到高；sort=-1,价格高到低；null默认created desc;
        // !!!!sort接收后是空字符串，或者1，-1
        if(pageInfo.getSort()== null){
            // 暂不清楚哪个地方发的请求中sort==null
            example.setOrderByClause("created asc");

        }else if(pageInfo.getSort().trim().equals("")){
            example.setOrderByClause("created desc");
        }else if("1".equals(pageInfo.getSort())){
            example.setOrderByClause("price");
        }else if("-1".equals(pageInfo.getSort())){
            example.setOrderByClause("price desc");
        }

        // if(pageInfo.getSort() != null && !pageInfo.getSort().trim().equals("") ){
        //     if (Integer.parseInt(pageInfo.getSort())==1) {
        //         example.setOrderByClause("price");
        //     }
        //     // if(pageInfo.getSort().equals("1")){
        //     //     example.setOrderByClause("price");
        //     // }
        //     else if(pageInfo.getSort().equals("-1")){
        //         example.setOrderByClause("price desc");
        //     }
        // }else{
        //     example.setOrderByClause("created desc");
        // }

        List<Item> items = itemMapper.selectByExample(example);
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);// 传list获取total
        List<ProductDto> productDtos = productConverter.items2Dto(items);//do-->productdto

        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setData(productDtos);
        allProductResponse.setTotal(itemPageInfo.getTotal());
        return allProductResponse;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {
        return null;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }

    @Override
    public CartProductDto getCartProductDto(Long productId) {



        return null;
    }
}
